import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {generatePageInfo, PageInfo} from '../../util';

@Component({
  selector: 'app-movie-list',
  templateUrl: './movie-list.component.html',
  styleUrls: ['./movie-list.component.css']
})
export class MovieListComponent implements OnInit {

  types: object;
  pageInfo: PageInfo = new PageInfo();
  selectedType = 0;

  constructor(private http: HttpClient) {
    this.http.get('/sys/param/flag/MovieType').subscribe(result => this.types = result);
    this.changeMovieListByType(1);
  }

  ngOnInit() {
  }

  changeMovieListByType(typeId: number) {
    this.selectedType = typeId;
    this.http.get('/movie/type/' + typeId + '/list?size=12', {observe: 'response'}).subscribe(result => {
      generatePageInfo(this.pageInfo, result);
    });
  }

  changePage(page: number) {
    this.pageInfo.currentPage = page;
    this.http.get('/movie/type/' + this.selectedType + '/list?page=' + page + '&size=12').subscribe(result => this.pageInfo.data = result);
  }
}
